<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/23 0023
 * Time: 下午 2:52
 */

namespace app\admin\model;


use think\Model;

class BannerModel extends Model
{
    protected $table = "banner";

    /**
     * 轮播图与文章一对一
     * @return \think\model\relation\HasOne
     */
    public function article()
    {
        return $this->hasOne("ArticleModel","id","article_id");
    }

    /**
     * 获取所有轮播图
     * @return array|Model[]
     */
//    static public function getBannerAll()
//    {
//        $model = new self();
//        $result = $model->order("sort","DESC")
//            ->select();
//        return $result ?? [];
//    }

    /**
     * 增加轮播图
     * @param $data
     * @return bool
     */
    static public function addBanner($data)
    {
        $model = new self();
        $result = $model->allowField(true)->save($data);
        return $result;
    }

    /**
     * 修改轮播图 article 一起
     * @param $data
     * @return bool
     */
    static public function editBanner($data)
    {
        $model = new self();
        $temp = ["bannerImg" => $data["bannerImg"]];
        $result = $model->allowField(true)->save($temp,["article_id" => $data["article_id"]]);
        return $result;
    }

    /**
     * 单独修改轮播图图片  出现顺序
     * @param $data
     * @return bool
     */
    static public function editBannerFind($data)
    {
        $model = new self();
        $temp = [
            "sort" => $data["sort"],
            "bannerImg" => $data["bannerImg"]
        ];
        $result = $model->save($temp,["article_id" => $data["article_id"]]);
        return $result;
    }

    /**
     * 获取一篇 轮播图
     * @param $article_id
     * @return BannerModel
     */
    static public function getBannerFind($article_id)
    {
        $result = self::where("article_id","=",$article_id)->find();
        return $result;
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return bool
     */
    static public function updateStatus($id,$status)
    {
        $model = new self();
        $temp = [
            "status" => $status == 1 ? 0 : 1
        ];
        $result = $model->save($temp,["article_id" => $id]);
        return $result;
    }
}